package com.qf.config;

import com.alibaba.druid.pool.DruidDataSource;
import com.mchange.v2.c3p0.ComboPooledDataSource;
import org.apache.commons.dbutils.QueryRunner;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.context.annotation.Scope;

import javax.sql.DataSource;
import java.beans.PropertyVetoException;

/**
 * @ClassName JdbcConfig
 * @Description TODO
 * @Author 86181
 * @Date 2022/8/30 21:12
 * @Version 1.0
 **/
@Configuration//指定当前类是一个配置类 当配置类作为AnnotationConfigApplicationContext
//对象创建参数时,该注解可以不写
//@ComponentScan("com.qf")
@PropertySource(value = "classpath:druid.properties")
public class JdbcConfig {
    @Value("${driverClassName}")
    private String driverClass;
    @Value("${url}")
    private String jdbcUrl;
    @Value("${username}")
    private String user;
    @Value("${password}")
    private String password;
    /**
     * 使用IOC容器管理QueryRunner
     */
    @Bean(name = "runner")
    /**
     * 将当前方法的返回值注入IOC容器
     */
    @Scope(value = "prototype")//将QueryRunner改为多例
    public QueryRunner queryRunner() {
        return new QueryRunner(dataSource());
    }

    @Bean(name = "druidDataSource")
    public DataSource dataSource() {
        DruidDataSource druidDataSource = new DruidDataSource();
       /// DataSource dataSource = new ComboPooledDataSource();
        // ((ComboPooledDataSource) dataSource).setDriverClass(driverClass);
        druidDataSource.setDriverClassName(driverClass);
        //  ((ComboPooledDataSource) dataSource).setJdbcUrl(jdbcUrl);
        druidDataSource.setUrl(jdbcUrl);
        //  ((ComboPooledDataSource) dataSource).setUser(user);
        druidDataSource.setUsername(user);
        //((ComboPooledDataSource) dataSource).setPassword(password);
        druidDataSource.setPassword(password);
        return druidDataSource;
    }

}
